package org.streets.eis.ext.rbac.component;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;

import com.inmethod.grid.column.AbstractColumn;
import org.streets.eis.types.PermitType;
import org.streets.eis.component.ChangedNotifyType;
import org.streets.eis.component.combobox.ListComboBox;
import org.streets.eis.component.grid.EditableColumn;
import org.streets.eis.component.model.ListItem;
import org.streets.eis.component.model.ListItemModel;

public class PermitColumn extends EditableColumn {
	private static final long serialVersionUID = 1L;
	
	private ChangedNotifyType changedNotifyType = ChangedNotifyType.NONE;

	@SuppressWarnings("unchecked")	
	public PermitColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty) {
		super(columnId, headerModel, propertyExpression, sortProperty);
	}

	@SuppressWarnings("unchecked")	
	public PermitColumn(String columnId, IModel headerModel, String propertyExpression) {
		super(columnId, headerModel, propertyExpression);
	}

	@SuppressWarnings("unchecked")	
	public PermitColumn(IModel headerModel, String propertyExpression, String sortProperty) {
		super(headerModel, propertyExpression, sortProperty);
	}

	@SuppressWarnings("unchecked")	
	public PermitColumn(IModel headerModel, String propertyExpression) {
		super(headerModel, propertyExpression);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected FormComponent newEditableComponent(String id, final IModel model, IModel rowModel, AbstractColumn column){
		return newPermitSelector(id,model).setRequired(true);
	}
	
	private List<ListItem> createPermitsModel(){
		List<ListItem> model = new ArrayList<ListItem>();
		model.add(new ListItem(String.valueOf(PermitType.REJECT.value()),PermitType.REJECT.getName()));
		model.add(new ListItem(String.valueOf(PermitType.READONLY.value()),PermitType.READONLY.getName()));
		model.add(new ListItem(String.valueOf(PermitType.NONE.value()),PermitType.NONE.getName()));
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected IModel getFieldModel(IModel rowModel) {
		return new ListItemModel(rowModel, getPropertyExpression()){
			private static final long serialVersionUID = 1L;
			
			@Override
			protected String getItemText(String id){
				if (String.valueOf(PermitType.REJECT.value()).equals(id)){
					return PermitType.REJECT.getName();
				}
				if (String.valueOf(PermitType.READONLY.value()).equals(id)){
					return PermitType.READONLY.getName();
				}
				if (String.valueOf(PermitType.NONE.value()).equals(id)){
					return PermitType.NONE.getName();
				}
				return id;
			}
		};
	}
	
	@SuppressWarnings("unchecked")	
	protected ListComboBox newPermitSelector(String id, final IModel model){
		return new ListComboBox(id, model,createPermitsModel(),false){			
			private static final long serialVersionUID = 1L;
			@Override
			protected Iterator<ListItem> getMatchedItems(String key) {				
				return null;
			}
			@Override
			protected ChangedNotifyType getChangedNotifyType(){
				return PermitColumn.this.getChangedNotifyType();
			}
			@Override
			protected void onNoPopup(AjaxRequestTarget target){
				PermitColumn.this.onNoPopup(this,target);
			}	
			@Override
			protected void onItemChanged(ListItem newValue){
				PermitColumn.this.onItemChanged(this,newValue);
			}
			@Override
			protected void onAjaxItemChanged(ListItem newValue,AjaxRequestTarget target){
				PermitColumn.this.onAjaxItemChanged(this,newValue,target);
			}
			
		};
	}

	public ChangedNotifyType getChangedNotifyType() {
		return changedNotifyType;
	}

	public void setChangedNotifyType(ChangedNotifyType changedNotifyType) {
		this.changedNotifyType = changedNotifyType;
	}	
		
	protected void onNoPopup(FormComponent<?> component,AjaxRequestTarget target){
	}
	
	protected void onItemChanged(FormComponent<?> component,ListItem newValue){		
	}

	protected void onAjaxItemChanged(FormComponent<?> component,ListItem newValue,AjaxRequestTarget target){
	}
	
}
